package friendManager;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import Bean.SqlBean;
import loginRegister.LoginBean;
import Bean.CheckBean;

/**
 *
 * @author 刘杉
 */
public class DeleteFriendServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws  IOException {
        String name=request.getParameter("Name");
        HttpSession session=request.getSession();
        LoginBean login=(LoginBean)session.getAttribute("login");
        if(login==null){
            response.sendRedirect("http://localhost:8080/login.jsp");
        }else{
            String userName="";
            userName=login.getUserName();
            try{
                ResultSet rs = null;
                SqlBean sb = new SqlBean();
                sb.connectSQL();
                String sql1="select * from friends where name='"+name+"'and userName='"+userName+"'";
                rs=sb.executeQuery(sql1);
                if(!rs.next()){
                    CheckBean.alert("删除失败!");
                    response.sendRedirect("http://localhost:8080/friendManager/deleteFriend.jsp");
                }else{
                    String sql2="delete from friends where name='"+name+"'and userName='"+userName+"'";
                    sb.executeUpdate(sql2);
                    String sql3="select * from friends where userName='"+userName+"'";
                    rs=sb.executeQuery(sql3);
                    rs.last();
                    int list=rs.getRow();
                    rs=sb.executeQuery(sql3);
                    if(list<1){
                        ArrayList friendslist=null;
                        session.setAttribute("friendslist", friendslist);
                    }else{
                    ArrayList friendslist=null;
                    friendslist=new ArrayList();
                    while(rs.next()){
                        FriendBean ff=new FriendBean();
                        ff.setName(rs.getString("name"));
                        ff.setPhone(rs.getString("phone"));
                        ff.setEmail(rs.getString("email"));
                        ff.setWorkPlace(rs.getString("workPlace"));
                        ff.setPlace(rs.getString("place"));
                        ff.setQQ(rs.getString("QQ"));
                        friendslist.add(ff);
                        session.setAttribute("friendslist", friendslist);
                      }
                   }
                    CheckBean.alert("此联系人已成功删除！");
                    sb.closeSQL();
                    response.sendRedirect("http://localhost:8080/friendManager/deleteFriend.jsp");
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws  IOException {
        doGet(request, response);
    }

}